﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_for_Auth.FramePart;

namespace WPF_for_Auth.PageS.RBAC
{
    /// <summary>
    /// Page_PageManager_Detail.xaml 的交互逻辑
    /// </summary>
    public partial class Page_PageManager_Detail : UserControl
    {
        public Page_PageManager_Detail()
        {
            InitializeComponent();
            this.DataContext = MvvmLoad.mvvm; // 设置数据上下文为 MvvmLoad 的实例
            this.Loaded += Page_PageManager_Detail_Loaded;
        }

        /// <summary>
        /// 页面加载事件处理程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Page_PageManager_Detail_Loaded(object sender, RoutedEventArgs e)
        {
            // 这里主要绑定页面的  PageWithControl
            var page = MvvmLoad.mvvm.App_DbOperate.CurrentEditPage;

            var t = FindName("Parameter_StackPanel_Content") as StackPanel;
            t.Children.Clear(); // 清空现有的子元素

            if (page.PageWithControls != null)
            {
                var list = page.PageWithControls.ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    AutoAddControl(t,list[i]);
                }
            }
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var t = FindName("Parameter_StackPanel_Content") as StackPanel;



            var userControl = new UserControl_Content_KeyValue
            {
                Id = "",
                Key = "自定义属性名称",
                Value = "自定义属性内容",
                Tag = new DB.Models.PageWithControl() { CreateTime = DateTime.Now, UpdateTime = DateTime.Now }
            };
            userControl.BackDelete += (s, e) =>
            {
                //MessageBox.Show("删除了");
                t.Children.Remove(userControl);
            };

            t.Children.Add(userControl);
            //MessageBox.Show($"{t.Children.Count}");

            var zz = this.DataContext;
        }

        private void AutoAddControl(StackPanel t, DB.Models.PageWithControl pwc)
        {
            

            var userControl = new UserControl_Content_KeyValue
            {
                Id = pwc.Id,
                Key = pwc.Name,
                Value = pwc.Description,
                Tag = pwc
            };
            userControl.BackDelete += (s, e) =>
            {
                //MessageBox.Show("删除了");
                t.Children.Remove(userControl);
            };

            t.Children.Add(userControl);
        }

        private void Button_Click_MakePageWithControlData(object sender, RoutedEventArgs e)
        {
            var t = FindName("Parameter_StackPanel_Content") as StackPanel;

            var page_with_controlS = MvvmLoad.mvvm.App_DbOperate.CurrentEditPage.PageWithControls;
            page_with_controlS.Clear();
            for (int i = 0; i < t.Children.Count; i++)
            {
                var item = t.Children[i] as UserControl_Content_KeyValue;
                page_with_controlS.Add(new DB.Models.PageWithControl() {
                    Name = item.Key,
                    Description = item.Value,
                    Id = item.Id == "" ? DB.Utils.Tools.NewGuid() : item.Id,
                    CreateTime = ((DB.Models.PageWithControl)item.Tag).CreateTime,
                    UpdateTime = ((DB.Models.PageWithControl)item.Tag).UpdateTime
                });
            }

            //MvvmLoad.mvvm.App_DbOperate.CurrentEditPage.PageWithControls = page_with_controlS;

        }
    }
}
